﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MaxAlarm.Core.Settings;
using MaxAlarm.ViewModels;

namespace MaxAlarm.Pages
{
    /// <summary>
    /// Interaction logic for LoginPage.xaml
    /// </summary>
    public partial class LoginPage : Page
    {
        public static readonly DependencyProperty ViewModelProperty =
            DependencyProperty.RegisterAttached("ViewModel", typeof(LoginPageViewModel), typeof(LoginPage),
                                                               new PropertyMetadata(null, PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {

        }

        public static void SetViewModel(UIElement element, LoginPageViewModel value)
        {
            element.SetValue(ViewModelProperty, value);
        }

        public static LoginPageViewModel GetViewModel(UIElement element)
        {
            return (LoginPageViewModel)element.GetValue(ViewModelProperty);
        }

        public LoginPageViewModel ViewModel
        {
            get { return (LoginPageViewModel)GetValue(ViewModelProperty); }
        }

        public LoginPage()
        {
            InitializeComponent();
        }


        private void PasswordChanged(object sender, RoutedEventArgs e)
        {
            var passBox = (PasswordBox)e.Source;
            ViewModel.Password = passBox.Password;
        }

        private void SelectedLoginChanged(object sender, SelectionChangedEventArgs e)
        {
            if (PasswordBox == null)
                return;

            var combo = (ComboBox) sender;
            var value = combo.SelectedValue;

            if (value == null)
            {
                PasswordBox.Password = null;
                return;
            }
                

            var strValue = (AccountSettingItem)value;

            PasswordBox.Password = !string.IsNullOrEmpty(strValue.Login)
                                       ? SettingsManager.Instance.Settings.AccountsSettings[strValue.Login]
                                       : null;
        }

    }
}
